package com.leyou.web;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.service.ISpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xiuer
 * @description 商品规格控制层
 * @create 2019/6/26 20:45
 */
@RestController
@RequestMapping("spec")
public class SpecController {
    @Autowired
    private ISpecService specService;

    /**
     * 根据商品分类id查询规格组
     *
     * @param cid //http://api.leyou.com/api/item/spec/groups/of/category/?id=3
     */
    @GetMapping("groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> querySpecGroup(@RequestParam("id") Long cid) {
        return ResponseEntity.ok(specService.querySpecGroupByid(cid));
    }

    /**
     * 根据 分类id查询规格组 和规格参数
     * @param cid
     * @return
     */
    @GetMapping("of/category")
    public ResponseEntity<List<SpecGroupDTO>> querySpecGroupAndParams(@RequestParam("cid") Long cid) {
        return ResponseEntity.ok(specService.querySpecGroupAndParams(cid));
    }

    /**
     * cid: 3, name: "书本大纲"}
     * http://api.leyou.com/api/item/spec/group
     * 新增规格组
     */
    @PostMapping("group")
    public ResponseEntity<Void> insertSpecGroup(@RequestBody SpecGroupDTO specGroupDTO) {
        specService.insertSpecGroup(specGroupDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    /**
     * http://api.leyou.com/api/item/spec/params?gid=1
     *
     * @param cid 根据分类cid查询规格参数
     * @Param gid 根据规格组gid查询规格参数
     * @Param
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> querySepcParams(
            @RequestParam(value = "gid", required = false) Long gid,
            @RequestParam(value = "cid", required = false) Long cid,
            @RequestParam(value = "searching", required = false) Boolean searching) {
        return ResponseEntity.ok(specService.querySepcParams(gid, cid, searching));
    }


    /**
     * 新增规格组参数
     * //http://api.leyou.com/api/item/spec/param
     * {cid: 3, groupId: 16, segments: "", numeric: false, searching: true, generic: true, name: "作者"}
     */
    @PostMapping("param")
    public ResponseEntity<Void> insertSpecParam(@RequestBody SpecParamDTO specParamDTO) {
        specService.insertSpecParam(specParamDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 规则参数的修改
     * {id: 5, cid: 76, groupId: 2, name: "机身重量（g）", numeric: true, unit: "gggg", generic: true,…}
     * http://api.leyou.com/api/item/spec/param
     *
     * @param specParamDTO
     * @return
     */
    @PutMapping("param")
    public ResponseEntity<Void> updateSepcParam(@RequestBody SpecParamDTO specParamDTO) {
        specService.updateSepcParam(specParamDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    //http://api.leyou.com/api/item/spec/params?cid=3


}

